package itbour.onetouchshow.fragment.movie;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import itbour.onetouchshow.base.mvp.BasePresenterImpl;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.SpUtils;

/**
 * Created by guoh on 2018/8/2.
 * 功能描述：
 * 需要的参数：
 */
public class ModulePartPresenter extends BasePresenterImpl<ModulePartView> {

    private int pageNo = 0;

    public void getModuleDetails(List<Integer> ids, String durationKey, String durationValue, String tmplTypeKey, String tmplTypeValue, boolean reload) {
        if (reload) {
            pageNo = 0;
        } else {
            pageNo += 1;
        }
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("setIds", new Gson().toJson(ids));
        stringStringHashMap.put("pageNum", pageNo);
        stringStringHashMap.put("pageSize", AppConst.PAGE_SIZE);
        stringStringHashMap.put("withTotalPage", 1);
        if (durationKey != null && durationValue != null) {
            stringStringHashMap.put(durationKey, durationValue);
        }
        if (tmplTypeKey != null && tmplTypeValue != null) {
            stringStringHashMap.put(tmplTypeKey, tmplTypeValue);
        }
        if (reload) {
            //刷新清除当前筛选条件下所有数据
            HashMap<String, String> results = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.PLAY_MOVIE_KEY), HashMap.class);
            if (results != null && results.size() > 0) {
                Iterator<Map.Entry<String, String>> iterator = results.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, String> entry = iterator.next();
                    String key = entry.getKey();
                    HashMap<String, Object> keys = new Gson().fromJson(key, HashMap.class);
                    keys.remove("pageNum");
                    keys.remove("withTotalPage");
                    keys.remove("pageSize");
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.putAll(stringStringHashMap);
                    temp.remove("pageNum");
                    temp.remove("withTotalPage");
                    temp.remove("pageSize");
                    if ((new Gson().toJson(temp)).equals(new Gson().toJson(keys))) {
                        iterator.remove();
                    }
                }

                SpUtils.getInstance().saveString(AppConst.PLAY_MOVIE_KEY, new Gson().toJson(results));
            }
        } else {
            //非刷新从缓存获取数据
            HashMap<String, String> hashMap = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.PLAY_MOVIE_KEY), HashMap.class);
            if (hashMap != null) {
                String result = hashMap.get(new Gson().toJson(stringStringHashMap));
                if (!TextUtils.isEmpty(result)) {
                    mvpView.obtainListSuccess(result, reload);
                    return;
                }
            }

        }

        HttpUtils.getInstance().executeGet(AppConfig.GETTMPLSETCONTENT_V1_0, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                if (mvpView != null) {
                    HashMap<String, String> hashMap = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.PLAY_MOVIE_KEY), HashMap.class);
                    if (hashMap == null) hashMap = new HashMap<>();
                    hashMap.put(new Gson().toJson(stringStringHashMap), response);
                    SpUtils.getInstance().saveString(AppConst.PLAY_MOVIE_KEY, new Gson().toJson(hashMap));
                    mvpView.obtainListSuccess(response, reload);
                }
            }
        });
    }

}
